package com.gopay.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gopay.checkorder.dao.BankOrderDAO;
import com.gopay.checkorder.dao.BcdlSendBankLogDAO;
import com.gopay.checkorder.dao.CheckBcdlSendBankLogDAO;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BcdlSendBankLog;
import com.gopay.common.domain.bank.CheckBcdlSendBankLog;
import com.gopay.common.util.ResponseUtils;

/**
 * 
 * @ClassName: CheckBcdlSendBankLogController 
 * @Description: CheckBcdlSendBankLog操作类 
 * @author sj 
 * @date 2018年2月6日 下午2:17:09
 */
@Controller
@Scope("prototype")
public class CheckBcdlSendBankLogController {
	private final static Logger logger = LoggerFactory.getLogger("CheckBcdlSendBankLogController");
	@Resource(name = "checkBcdlSendBankLogDAO")
	CheckBcdlSendBankLogDAO checkBcdlSendBankLogDAO;
	@Resource(name = "bcdlSendBankLogDAO")
	BcdlSendBankLogDAO bcdlSendBankLogDAO;
	@Resource(name = "bankOrderDAO")
	BankOrderDAO bankOrderDAO;
	
	private String resultString;
	
	@RequestMapping(value = "/deleteCheckBcdlSendBankLog", method = { RequestMethod.GET })
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) {
		String gopayOrderId = request.getParameter("gopayOrderId");
		logger.error("传入参数orderId为" + gopayOrderId);
		
		resultString = "";
		if(isExistCheckBcdlSendBankLog(gopayOrderId)
				&& isNotExistBcdlSendBankLog(gopayOrderId)
				&& isOrderStatusAccepted(gopayOrderId)){
			
			int num = checkBcdlSendBankLogDAO.delLogByGopayOrderId(gopayOrderId );
			
			resultString = gopayOrderId + "===check银企日志表删除成功！共删除" + num + "条数据"  ;
		}else{
			resultString = gopayOrderId + "===check银企日志表删除失败！" + resultString;
		}
		logger.error(resultString);
		ResponseUtils.renderText(response, resultString);
		
	}
	
	/**
	 * @Description: check银企日志表是否有记录
	 * @param gopayOrderId
	 * @return 
	 * @author admin
	 */
	private boolean isExistCheckBcdlSendBankLog(String gopayOrderId){
		resultString += "\n查询checkorder_bcdl_send_bank_log是否有记录：";
		CheckBcdlSendBankLog checkBcdlSendBankLog = checkBcdlSendBankLogDAO.getLogByGopayOrderId(gopayOrderId);
		if(checkBcdlSendBankLog == null){
			resultString += "没有记录！" + wrongAlert();
			return false;
		}
		resultString += "有记录！" + rightAlert();
		return true;
	}
	
	/**
	 * @Description: bis银企日志表是否有记录
	 * @param gopayOrderId
	 * @return 
	 * @author admin
	 */
	private boolean isNotExistBcdlSendBankLog(String gopayOrderId){
		resultString += "\n查询BIS_BCDL_SEND_BANK_LOG是否有记录：";
		BcdlSendBankLog bcdlSendBankLog = bcdlSendBankLogDAO.getLogByGopayOrderId(gopayOrderId);
		if(bcdlSendBankLog != null){
			resultString += "有记录" + wrongAlert();
			return false;
		}
		resultString += "没有记录" + rightAlert();
		return true;
	}
	
	/**
	 * @Description: bis主订单表的记录状态是否是11
	 * @param gopayOrderId
	 * @return 
	 * @author admin
	 */
	private boolean isOrderStatusAccepted(String gopayOrderId){
		resultString += "\n查询BIS_BANK_MAIN_ORDER里面订单状态是否为11：";
		BankOrder bankOrder = bankOrderDAO.getByGopayOrderId(gopayOrderId);
		if(bankOrder == null){
			resultString += "订单不存在" + wrongAlert();
			return false;
		}
		if(!BankOrderStatus.ACCEPTED.equals(bankOrder.getOrderStatus())){
			resultString += "订单状态是" + bankOrder.getOrderStatus() + wrongAlert();
			return false;
		}
		resultString += rightAlert();
		return true;
	}
	
	private String rightAlert(){
		return "   （通过)";
	}
	
	private String wrongAlert(){
		return "    (不通过)";
	}
}
